/*
 *  Copyright (c) 2011 Lukáš Karas <lukas.karas@centrum.cz>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation;
 *
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
 *
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package cz.karry.jtw.tests;

/**
 *
 * @author karry
 */
public class LockedThreads {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    final Object lock = new Object();

    (new Thread(new Runnable() {

      public void run() {
        System.out.println(Thread.currentThread().getName()+" started");
        while (true) {
          synchronized (lock) {
            //System.out.println("A... 1");
          }
          //System.out.println("A... 2");
        }
      }
    }, "PoorThread")).start();


    Object o = new Object();
    while (true) {
      synchronized (lock) {
        System.out.println(Thread.currentThread().getName()+" take lock");
        synchronized (o) {
          try {
            o.wait(10000l);
          } catch (InterruptedException ex) {
          }
        }
      }
      System.out.println(Thread.currentThread().getName()+" release lock");

      synchronized (o) {
        try {
          o.wait(1000l);
        } catch (InterruptedException ex) {
        }
      }
    }
  }
}
